#!/bin/sh
# PCP QA Test No. 1334
# Check sample-proc archives.
#
# Copyright (c) 2021 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

# pmdumplog instance domain timestamps
#   14:32:11.332428 InDom: 2.1 1 instances
#   14:32:11.332428	      0          132          132
#
# hostname from lines like
#   Performance metrics from host bozo.localdomain
#
# drop instance and values from pmcd.* metrics
#   2.3.3 (pmcd.pmlogger.host): inst [1955025 or "1955025"] value "bozo.localdomain"
#
# dates and times from label lines
#   commencing Tue Aug  3 14:32:11.332428 2021
#   ending     Tue Aug  3 14:32:16.102999 2021
#   Archive timezone: AEST-10
#
# pmlogger PID lines
#   PID for pmlogger: 1955025
#   1955025 or "1955025"
#
# temporal index entries and preamble pmResult size depend on the
# length of the hostname
#
# and non-determinism in sample.proc.time values
#
_filter()
{
    tee $tmp.out \
    | sed -n -e '/^[0-2][0-9]:/s/[ 	].*//p' \
    | LC_COLLATE=POSIX sort -n \
    | uniq \
    | $PCP_AWK_PROG >$tmp.sed '
BEGIN	{ ns = 1; last = "" }
	{ if ($1 ~ /[0-9][0-9][0-9][0-9][0-9][0-9]000$/) {
	    # nsec timestamp, but with usec precision
	    save = $1
	    sub(/000$/, "0*", $1)
	    sub(/000$/, "", save)
	    if (save == last)
		ns--
	  }
	  print "s/^" $1 "\\([ 	]\\)/STAMP-" ns "\\1/"
	  ns++
	  last = $1
	}'
    echo "--- tmp.sed ---" >>$seq_full
    cat $tmp.sed >>$seq_full
    echo "--- tmp.out ---" >>$seq_full
    cat $tmp.out >>$seq_full
    sed -f $tmp.sed $tmp.out \
    | sed \
	-e "s/ `hostname`/ LOCALHOST/g" \
	-e "s/ `hostname -s`/ LOCALHOST/g" \
	-e '/(pmcd\..*):/s/): .*/): .../' \
	-e '/ commencing /s/ing .*/ing .../' \
	-e '/ ending /s/ing .*/ing .../' \
	-e '/ timezone: /s/:.*/: .../' \
	-e '/PID for pmlogger:/s/: .*/: PID/' \
	-e 's/[0-9][0-9]* or "[0-9][0-9]*"/PID or "PID"/' \
	-e '/^STAMP-[0-9][0-9]*	/s/  *[1-9][0-9][0-9][0-9]*/ NNNN/g' \
	-e '/^\[[1-2][0-9][0-9] bytes]/s//[NNN bytes]/' \
	-e '/^Archive zoneinfo:/s/: :.*/: .../' \
    | $PCP_AWK_PROG '
BEGIN				{ filter = 0 }
/29.0.157 .sample.proc.exec.:/	{ filter = 0 }
filter == 1			{ if ($NF != 0) gsub(/[0-9][0-9]*$/,"TIME") }
				{ print }
/29.0.158 .sample.proc.time.:/	{ filter = 1 }'
}

# real QA test starts here
echo "=== V2 ==="
( pmdumplog -a tmparch/sample-proc_v2 2>&1 \
  ; pmdumplog -I tmparch/sample-proc_v2 2>&1 \
) | _filter

echo
echo "=== V3 ==="
( pmdumplog -a tmparch/sample-proc_v3 2>&1 \
  ; pmdumplog -I tmparch/sample-proc_v3 2>&1 \
) | _filter

# success, all done
exit
